<?php

namespace App\Models\Home;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;

class Property extends Model
{
    use Filterable;
    //对应文章动态
    public function articles()
    {
        return $this->hasMany('App\Models\Article');
    }

    //分类下所有的相册
    public function album()
    {
        return $this->hasMany('App\Models\Album');
    }

    //楼盘下所有的户型
    public function apartments()
    {
        return $this->hasMany('App\Models\Apartment');
    }

    public function getFeaturetxtAttribute($value)
    {
        return json_decode($value);
    }

    public function getImagesAttribute($value)
    {
        return unserialize($value);
    }

    public function getSalestatusAttribute($value)
    {
        if($value == 'T'){
            return '在售';
        }elseif($value == 'F'){
            return '停售';
        }elseif($value == 'W'){
            return '待售';
        }
    }

    public function scopeValid($query,$sort)
    {
        switch ($sort)
        {
            case 'asc':
                $model = $query->orderBy('average','asc');
                break;
            case 'desc':
                $model = $query->orderBy('average','desc');
                break;
            case 'popular':
                $model = $query->orderBy('follow','desc');
                break;
            default:
                $model = $query->orderBy('created_at','desc');
        }
        return $model;
    }


}
